import axios from 'axios'

axios.defaults.timeout = 5000;                        //响应时间
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';        //配置请求头
// axios.defaults.headers.post['Cache-Control'] = 'no-cache';        //配置请求头
// TODO axios请求基础端口
// axios.defaults.baseURL = 'http://localhost:3000';   //配置接口地址

//返回状态判断(添加响应拦截器)
axios.interceptors.response.use((resp: any) => {
    //对响应数据做些事
    if (!resp.data.success) {
        return Promise.resolve(resp);
    }
    return resp;
}, (error: any) => {
    console.log('网络异常...')
    return Promise.reject(error);
});

//返回一个Promise(发送post请求)
export function post(url: string, params: object) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then((resp: any) => {
                resolve(resp);
            }, (err: any) => {
                reject(err);
            })
            .catch((error: any) => {
                reject(error)
            })
    })
}
////返回一个Promise(发送get请求)
export function get(url: string, param: any) {
    return new Promise((resolve, reject) => {
        axios.get(url, { params: param })
            .then((resp: any) => {
                resolve(resp)
            }, (err: any) => {
                reject(err)
            })
            .catch((error: any) => {
                reject(error)
            })
    })
}
export default {
    post,
    get,
}